# import requests
# import time
# from multiprocessing import Process, Pool
# from threading import Thread
#
# result = []
#
#
# def request_baidu(index):
#     url = "https://www.baidu.com/s"
#     request_body = ""
#     headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Sa"}
#
#     response = requests.get(f"{url}?{request_body}", headers = headers)
#     time.sleep(1)
#     print(f"{index} request, response status:{response.status_code}")
#     result.append(response.status_code)
#     # print(f"{index} request, get result {result}")
#     return response.status_code


# if __name__ == '__main__':
#     p_array = []
#     for i in range(10):
#         p = Process(target=request_baidu, args=(1,))
#         p.start()
#         p_array.append(p)
#
#     for p in p_array:
#         p.join()
#     print("done!!!")

# start_time = time.time()
# for i in range(10):
#     request_baidu()
# end_time = time.time()
# print(f"执行时间：{end_time - start_time}s")


# if __name__ == '__main__':
#     pool = Pool(8)
#     f_array = []
#     for index in range(8):
#         future = pool.apply_async(request_baidu, args=(index,))
#         f_array.append(future)
#     pool.close()
#     pool.join()
#     for f in f_array:
#         print(f.get())
#
# if __name__ == '__main__':
#     t_array = []
#     for index in range(10):
#         t = Thread(target=request_baidu, args=(index, ))
#         t.start()
#         t_array.append(t)
#
#     [t.join() for t in t_array]
#     print("done!!!")
#     print(result)


import requests
import time
from multiprocessing import Process, Pool
from threading import Thread

# thread_result = []
# process_result = []
result = []


def clock_it_deco(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result_time = func(*args, **kwargs)
        end_time = time.time()
        print(f"执行时间： {format(end_time - start_time, '.2f')} s")
        return result_time
    return wrapper


def request_baidu(index):
    url = "https://www.baidu.com/s"
    request_body = ""
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Sa"}

    response = requests.get(f"{url}?{request_body}", headers = headers)
    time.sleep(1)
    print(f"{index} request, response status:{response.status_code}")
    result.append(response.status_code)
    # print(f"{index} request, get result {result}")
    return response.status_code


@clock_it_deco
def process_func():
    p_array = []
    for i in range(10):
        p = Process(target=request_baidu, args=(i,))
        p.start()
        p_array.append(p)

    for p in p_array:
        p.join()
    print("process done!!!")


@clock_it_deco
def thread_func():
    t_array = []
    for index in range(10):
        t = Thread(target=request_baidu, args=(index, ))
        t.start()
        t_array.append(t)

    [t.join() for t in t_array]
    print("thread done!!!")
    print(result)


if __name__ == '__main__':
    process_func()
    thread_func()



